import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.*;
import java.sql.*;

public class SThreadPublish extends Thread{

	 private Socket socket = null;

	    public SThreadPublish(Socket socket) {
		super("Publish Thread");
		this.socket = socket;

}

	    public void run() {

	    	String information=null, filename=null, username=null, hash=null,first=null,ip=null, time=null,filesize=null;
	    	int pointer=0;
	    	try {
	    		
	    	    PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
	    	    BufferedReader in = new BufferedReader(
	    				    new InputStreamReader(
	    				    socket.getInputStream()));
	    	    
	    	    out.write("T|Send|" + '\n');
	    	    System.out.println("T|Send|" + '\n');
	    	    out.flush();
	    	   
	    	   /* try
	    	    {
	    	    while(!in.ready())
	    	    {
						SThreadPublish.sleep(50);

	    	    }
	    	   } catch (InterruptedException e) {
	    	    	
				}*/
	    	

	    	   information=in.readLine();
	    	    
	    	    System.out.println("!!"+information+"!");
	    	   try
	    	   {
	    	   while(!information.startsWith("P"))
	    	   {
	    	    try{
	    	    	information=information.substring(2);
	 	    	    pointer=information.indexOf("|");
	 	    	    filename=information.substring(0, pointer);
	 	    	    System.out.println("filename: "+filename);
	 	    	    information=information.substring(pointer+1);
	 	    	    pointer=information.indexOf("|");
	 	    	    first=information.substring(0, pointer);
	 	    	    System.out.println("first: "+first);
	 	    	    information=information.substring(pointer+1);
	 	    	    pointer=information.indexOf("|");
	 	    	    hash=information.substring(0, pointer);
	 	    	    System.out.println("Hash: "+hash);
	 	    	    information=information.substring(pointer+1);
	 	    	    pointer=information.indexOf("|");
	 	    	    username=information.substring(0, pointer);
	 	    	    System.out.println("username: "+username);
	 	    	    information=information.substring(pointer+1);
	 	    	    pointer=information.indexOf("|");
	 	    	    ip=information.substring(0, pointer);
	 	    	    System.out.println("ip: "+ip);
	 	    	    information=information.substring(pointer+1);
	 	    	    pointer=information.indexOf("|");
	 	    	    filesize=information.substring(0, pointer);
	 	    	    System.out.println("filesize: "+filesize);
	 	    	    information=information.substring(pointer+1);
	 	    	    pointer=information.indexOf("|");
	 	    	    time=information.substring(0, pointer);
	 	    	    System.out.println("time:"+time);
	    	    }
	    	    catch(Exception e )
	    	    {
	    	    	 out.write("!|1| " + e.toString() + "|" + '\n');
	    	    }
	    	    
	    	    
	    	    try {
	    	    	PreparedStatement SQLstmt;
	    	        Class.forName("com.mysql.jdbc.Driver");
	    	        String url = "jdbc:mysql://localhost:3306/mysql";

	    	        Connection con = DriverManager.getConnection(url,"root", "admin");

	    	        SQLstmt = con.prepareStatement("INSERT INTO organize.publish (FileName,Digest,Hash,username,IPAddress,FileSize,TimeModified)"
    	        			+ " VALUES (?, ?, ?, ?, ?, ?, ?)");
	    	        
	    	        /* String in mysql SQL command must be escaped.
	    	         *  http://dev.mysql.com/doc/refman/5.0/en/string-syntax.html
	    	         * 
	    	         * PreparedStatement could escape string for you.
	    	         */
	    	        SQLstmt.setString(1, filename);
	    	        SQLstmt.setString(2, first);
	    	        SQLstmt.setString(3, hash);
	    	        SQLstmt.setString(4, username);
	    	        SQLstmt.setString(5, ip);
	    	        SQLstmt.setString(6, filesize);
	    	        SQLstmt.setString(7, time);
	    	        
	    	        SQLstmt.execute();

	    	        
	    	        out.write("T|Done|"+'\n');
	    	        SQLstmt.close();
		    	    con.close();
		    	    
	    	      }catch( Exception e ) {
	    	          out.write("!|11|Could not access database|" + e.toString()+ "|" +'\n');
	    	     }

	    	      out.flush();
	    		  
	    	      try
		    	    {
		    	    while(!in.ready())
		    	    {
							SThreadPublish.sleep(50);

		    	    }
		    	   } catch (InterruptedException e) {
		    	    	
					}
		    	   
	    	      information=in.readLine();
	    	    
	    	   }
	    	   }
	    	   catch(Exception e)
	    	   {
	    		   e.printStackTrace();
	    	   }
	    	   
	    	    out.write("P|Done|"+'\n');
	    	   	
	    	    out.close();
	    	    in.close();
	    	    socket.close();

	    	} catch (IOException e) {
	    	    e.printStackTrace();
	    	}
	        }
	    
}
